﻿using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using RaytracerLib.MathLib;
using TomanuExtensions;

namespace Raytracer.PropertyGridDecorators
{
    public class Vector3Converter : TypeConverter
    {
        public override bool CanConvertFrom(ITypeDescriptorContext a_context,
            Type a_source_type)
        {
            if (a_source_type == typeof(string))
                return true;

            return base.CanConvertFrom(a_context, a_source_type);
        }

        public override bool CanConvertTo(ITypeDescriptorContext a_context,
            Type a_destination_type)
        {
            if (a_destination_type == typeof(Vector3))
                return true;

            return base.CanConvertTo(a_context, a_destination_type);
        }

        public override object ConvertFrom(ITypeDescriptorContext a_context,
            CultureInfo a_culture, object a_value)
        {
            if (a_value is string)
            {
                var ar = (a_value as string).Split(new[] { ',' }).Select(
                    s => s.ToSingle()).ToArray();

                if (ar.Length != 3)
                    throw new FormatException();

                Vector3 v = new Vector3(ar[0], ar[1], ar[2]);

                return new Vector3Properties(() => v, (r) => Debug.Fail(""));
            }

            return base.ConvertFrom(a_context, a_culture, a_value);
        }

        public override object ConvertTo(ITypeDescriptorContext a_context,
            CultureInfo a_culture, object a_value, Type a_destination_type)
        {
            if (a_destination_type == typeof(string))
            {
                if (a_value is Vector3Properties)
                {
                    var v = a_value as Vector3Properties;
                    return String.Format(CultureInfo.InvariantCulture, "{0}, {1}, {2}",
                        v.X, v.Y, v.Z);
                }
                else if (a_value is Vector3ReadonlyProperties)
                {
                    var v = a_value as Vector3ReadonlyProperties;
                    return String.Format(CultureInfo.InvariantCulture, "{0}, {1}, {2}",
                        v.X, v.Y, v.Z);
                }
                else if (a_value is Vector3)
                {
                    var v = (Vector3)a_value;
                    return String.Format(CultureInfo.InvariantCulture, "{0}, {1}, {2}",
                        v.X, v.Y, v.Z);
                }
            }

            return base.ConvertTo(a_context, a_culture, a_value, a_destination_type);
        }

        public override bool GetPropertiesSupported(ITypeDescriptorContext a_context)
        {
            return true;
        }

        public override PropertyDescriptorCollection GetProperties(
            ITypeDescriptorContext a_context, object a_value, Attribute[] a_attributes)
        {
            return TypeDescriptor.GetProperties(a_value, a_attributes);
        }
    }
}